home *** CD-ROM | disk | FTP | other *** search
- /* Copyright, 1990, Regents of the University of Colorado */
- /******************************************************************
- *
- * This program reads a DINO program that has had the comments
- * stripped out by /lib/cpp and the line information removed by
- * dinoline and returns the names of all the environment
- * structures. It implicitly assumes that the program is not
- * too badly mangled (it returns the word following each
- * occurence of "environment").
- *
- ******************************************************************/
-
- #include <memory.h>
- #include <stdio.h>
- #include <errno.h>
- #include <fcntl.h>
-
- #define BOOL char
- #define TRUE 1
- #define FALSE 0
-
- #define MAXENVS 10
- #define MAXNAMESIZE 128
-
- static char buffer[2048];
- static char *bufptr;
- static char *endptr;
- static int lastcnt;
- static BOOL end = FALSE;
- static int fd;
-
- void refreshbuf();
-
- extern void exit();
- extern char *strncpy();
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int I;
- char envs[MAXENVS][MAXNAMESIZE];
- int nenvs = 0;
- int bytes;
- char *temp;
-
- /* Open the source file for reading. */
-
- if ((fd = open(argv[1], O_RDONLY)) == -1)
- {
- perror("Environment extraction");
- exit(1);
- }
-
- /* Fill the read buffer and set the pointers. */
-
- bytes = read(fd, buffer, 2048);
- if (bytes < 0)
- {
- perror("Environment extraction");
- exit(1);
- }
- bufptr = buffer;
- if (bytes < 1024)
- {
- endptr = buffer + bytes;
- end = TRUE;
- }
- else
- {
- endptr = buffer + 1024;
- lastcnt = bytes - 1024;
- }
-
- /* Loop for as long as there is more than
- can be put into the read buffer. */
-
- for(;;)
- {
- /* Look for an "e". */
-
- for (;bufptr != endptr && *bufptr != 'e'; bufptr++);
-
- /* If we reached the halfway point in the buffer, refill it. */
-
- if (bufptr == endptr)
- {
- if (end)
- break;
- else
- {
- refreshbuf();
- continue;
- }
- }
-
- /* Check that the character after the "e"
- is not at the halfway point. */
-
- if (++bufptr == endptr)
- {
- if (end)
- break;
- else
- refreshbuf();
- }
-
- /* If the character after the "e" is not an "n",
- go back to the beginning of the loop. */
-
- if (*bufptr != 'n')
- continue;
-
- /* Check that the character after the "n"
- is not at the halfway point. */
-
- if (++bufptr == endptr)
- {
- if (end)
- break;
- else
- refreshbuf();
- }
-
- /* If the character after the "n" is not a "v",
- go back to the beginning of the loop. */
-
- if (*bufptr != 'v')
- continue;
-
- /* Check that the character after the "v"
- is not at the halfway point. */
-
- if (++bufptr == endptr)
- {
- if (end)
- break;
- else
- refreshbuf();
- }
-
- /* Look to see if the rest of
- "environment" follows the "env". */
-
- if (memcmp(bufptr, "ironment", 8) != 0)
- continue;
-
- /* If so, move the pointer past the "environment" and if
- we have past the halfway point, refresh the buffer. */
-
- bufptr += 8;
- if (bufptr >= endptr)
- {
- if (end)
- break;
- else
- refreshbuf();
- }
-
- /* Skip all spaces, tabs, and newlines. */
-
- for(;;)
- {
- bufptr++;
- if (bufptr == endptr)
- {
- if (end)
- break;
- else
- refreshbuf();
- }
- if (*bufptr != ' ' && *bufptr != '\t' && *bufptr != '\n')
- break;
- }
- if (bufptr == endptr)
- break;
-
- /* Find the end of the word after "environment". */
-
- for(temp = bufptr;;)
- {
- temp++;
- if (*temp == ' ' || *temp == '\t' || (temp == endptr && end) ||
- *temp == '\n' || *temp == '[' || *temp == '{')
- break;
- }
-
- /* Copy that word into the env table. */
-
- (void) strncpy(envs[nenvs], bufptr, temp - bufptr);
- envs[nenvs++][temp - bufptr] = '\0';
-
- /* Reset the buffer pointer. */
-
- bufptr = temp;
- if (bufptr >= endptr)
- {
- if (end)
- break;
- else
- refreshbuf();
- }
- }
-
- /* Print out the env table. */
-
- for (I = 0; I < nenvs; I++)
- {
- (void) printf("%s", envs[I]);
- if (I < nenvs - 1)
- (void) printf(" ");
- }
-
- (void) close(fd);
-
- return(0);
- }
-
- void refreshbuf()
- {
- bufptr -= 1024;
- (void) memcpy(buffer, endptr, lastcnt);
- if (lastcnt < 1024)
- {
- end = TRUE;
- endptr = buffer + lastcnt;
- }
- else
- lastcnt = read(fd, endptr, 1024);
- }
-
-